begincreaturescript;

variables;

short i,target;
short talk1 = 0;
short talk2 = 0;
short talk3 = 0;
short following = 0;
short dead_serv = 0;

body;

beginstate INIT_STATE;
	set_name(ME,"Mehken");
	set_char_unkillable(ME,1);
	change_max_health(ME,200);
	set_attack_bonus(ME,2);
	
	break;

beginstate DEAD_STATE;
break;

beginstate START_STATE; 

	if (gf(0,25) == 0)
		end();
	
	// once final cutscene, this char is done
	if (gf(0,1) > 0)
		end();
		
	sf(0,2,0);
	if (dist_to_pc() <= 8)
		sf(0,2,1);
	
	// first greeting
	if (gf(0,29) == 0) {
		if (dist_to_pc() <= 8)
			approach_char(ME,pc_num(),2);
		if (dist_to_pc() <= 3) {
			sf(0,29,1);
			talk_no_exit(30);
			}
		}
	if (gf(0,29) == 1) {
		if (get_ran(1,0,100) < 20)
			give_char_text_bubble(ME,"Follow!");
		if (get_ran(1,0,100) < 20)
			give_char_text_bubble(ME,"Pacify the rogues!!");
		approach_nav_point(ME,0,2);
		if (dist_to_nav_point(ME,0) <= 3)
			sf(0,29,2);
		}
	if ((gf(0,29) == 2) && ((dist_to_pc() <= 4) || (gf(1,21) > 0))) {
		sf(0,8,1);
		sf(0,29,3);
		
		set_attitude(1001,10);
		set_attitude(1002,10);
		
		// eliminate excess bugs
		erase_char(22);
		erase_char(24);
		erase_char(18);
		erase_char(19);
		inc_flag(0,10,4);
		
		talk_no_exit(40);
		}
	if ((gf(0,29) == 3) && (gf(0,10) > 13) && (dist_to_pc() <= 6)) {
		sf(0,29,4);
		activate_hint(13);
		talk_no_exit(60);
		}
	if ((gf(0,29) == 4) && (is_combat()) && (gf(0,11) < 2)) {
		activate_hint(13);
		}
	if (gf(0,29) == 4) {
		following = 1;
		}
	
	// convos
	if (gf(0,11) == 1) {
		sf(0,11,2);
		talk_no_exit(50);
		}
	if (gf(0,14) == 1) {
		sf(0,14,2);
		talk_no_exit(80);
		}	
	if (gf(0,15) == 1) {
		sf(0,15,2);
		talk_no_exit(90);
		}	
	
	if ((is_group()) && (dead_serv == 0) && (gf(0,2) > 0) && (dist_to_nav_point(ME,3) <= 4)) {
		dead_serv = 1;
		talk_no_exit(115);
		}
	if ((talk1 == 0) && (gf(0,2) > 0) && (gf(0,24) == 0) && (dist_to_nav_point(ME,4) <= 5)) {
		talk1 = 1;
		talk_no_exit(150);
		}

		
	// if I have a target for some reason, go attack it
		if (target_ok()) {
			if (dist_to_char(get_target()) <= 16)
				set_state(3);
				else set_foe_target(ME,-1);
			}
		
		if (get_foe_target(ME,8,0)) {
			do_attack();
			set_state(3);
			}
		if (who_shot_me() >= 0) {
			set_foe_target(ME,who_shot_me());
			do_attack();
			set_state(3);
			}
	
	// extra check to make sure mahken fights final boss
	if ((gf(0,24) > 0) && (can_see_char(50))) {
		set_foe_target(ME,50);
		do_attack();
		set_state(3);
		}
		
	if (following) {
		if (dist_to_pc() > 12) {
			print_str("Mehken runs to catch up with you.");
			telep_char_to_char(ME,pc_num(),FALSE);
			}
			else {
				if (dist_to_pc() <= 2)
					fidget(ME,15);
					else maintain_dist_to_char(ME,pc_num(),1);
				}
		}
		else if (gf(0,29) == 3) // stay close to pc in bug fight
			maintain_dist_to_char(ME,pc_num(),2);
			
	if (am_i_doing_action() == FALSE)
		end_combat_turn();

break;

beginstate 3; // attacking
	sf(0,2,0);
	if (dist_to_pc() <= 8)
		sf(0,2,1);

	// once final cutscene, this char is done
	if (gf(0,1) > 0)
		end();


	// convos
	if (gf(0,11) == 1) {
		sf(0,11,2);
		talk_no_exit(50);
		}
	if (gf(0,14) == 1) {
		sf(0,14,2);
		talk_no_exit(80);
		}
	if (gf(0,15) == 1) {
		sf(0,15,2);
		talk_no_exit(90);
		}

	if ((talk1 == 0) && (gf(0,2) > 0) && (gf(0,24) == 0) && (dist_to_nav_point(ME,4) <= 5)) {
		talk1 = 1;
		talk_no_exit(150);
		}
	
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((following > 0) && (dist_to_pc() >= 10)) {
		set_foe_target(ME,-1);
		maintain_dist_to_char(ME,pc_num(),2);
		set_state(START_STATE);
		}


	do_attack();
break;

beginstate TALKING_STATE;
	if (gf(0,29) == 0)
		print_str("Mehken is walking over to talk to you.");
	if (gf(0,29) == 1)
		print_str("Mehken is hunting for rogues.");
	if (gf(0,29) > 1)
		print_str("Mehken says, 'We can talk soon. Now, we must hunt.'");

break;